<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新浪微博爬虫兼搜索引擎</title>
</head>
 <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
<script src="/static/layui/layui.all.js"></script>
<style>
    font{
        color:red;
    }
</style>
<body>
<div class="demoTable">
  <div class="layui-inline">
    <input id="keyword" type="text" name="title" lay-verify="title"  autocomplete="off" placeholder="请输入关键字" class="layui-input" >
  </div>
    <button class="layui-btn" data-type="reload">搜索</button>
    <div class="layui-inline" id="hotWord">

    </div>
</div>
<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">爬取微博内容</button>
    <button class="layui-btn layui-btn-sm" lay-event="clearData">清空重置数据</button>
{#    <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>#}
  </div>
</script>

<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script>
layui.use(['jquery','table'], function(){
  var table = layui.table;
 var $ = layui.$ //重点处
  $("#keyword").keyup(function () {
      $.ajax({
            url: "/hotwords/getHotWords",
            success: function(response){
                $("#hotWord").empty()
                for (let i = 0; i < response.length; i++) {
                    {#$("#hotWord").append('<q style="font-color:red">'+response[i].hotWord+'</q>')#}
                }
            },
        });
    });
  table.render({
    elem: '#test'
    ,url:'/xlSpider/getDataFromDB'
    ,where:{"keyword":$("#keyword").val()}
    ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
    ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
      title: '提示'
      ,layEvent: 'LAYTABLE_TIPS'
      ,icon: 'layui-icon-tips'
    }]
     ,id: 'testReload'
    ,title: '爬取内容结果'
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field:'score', title:'访问次数', width:200, fixed: 'left', unresize: true, sort: true}
      ,{field:'content', title:'内容', width:300, edit: 'text'}
      ,{field:'url', title:'访问路径', width:300, edit: 'text',templet: function(res){
        return '<a href="'+ res.url+'">'+ res.url +'</a>'
      } }
    ]]
    ,page: true
  });

  //头工具栏事件
  table.on('toolbar(test)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
      case 'getCheckData':
        $.ajax({
            url: "/xlSpider/createSpider",
            success: function(response){
                alert(response)
                reload();
            },
        });
      break;
      case 'clearData':
         $.ajax({
            url: "/xlSpider/clearData",
            success: function(response){
                alert(response)
            },
        });
      break;
      case 'isAll':
        layer.msg(checkStatus.isAll ? '全选': '未全选');
      break;

      //自定义头工具栏右侧图标 - 提示
      case 'LAYTABLE_TIPS':
        layer.alert('这是工具栏右侧自定义的一个图标按钮');
      break;
    };
  });

   var $ = layui.$, active = {
    reload: function(){
      var demoReload = $('#keyword');

      //执行重载
      table.reload('testReload', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
          keyword: demoReload.val()
        }
      }, 'data');
    }
  };

    $('.demoTable .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });

  //监听行工具事件
  table.on('tool(test)', function(obj){
    var data = obj.data;
    //console.log(obj)
    if(obj.event === 'del'){
      layer.confirm('真的删除行么', function(index){
        obj.del();
        layer.close(index);
      });
    } else if(obj.event === 'edit'){
      layer.prompt({
        formType: 2
        ,value: data.email
      }, function(value, index){
        obj.update({
          email: value
        });
        layer.close(index);
      });
    }
  });
});
</script>
</body>
</html>